package cc.chenwenxi.mypi.appium;

import cc.chenwenxi.mypi.appium.kits.AppiumUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.extra.spring.SpringUtil;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import lombok.extern.slf4j.Slf4j;
import org.mapdb.DB;
import org.mapdb.HTreeMap;
import org.openqa.selenium.support.PageFactory;

import java.util.Date;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Slf4j
public class 巴乐兔AppiumTask implements AppiumTask {
    private static final Activity mainActivity = new Activity("com.haimai.baletu","com.haimai.main.activity.SplashActivity");

    private final AndroidDriver driver;
    private final AppiumUtils kit;

    private final DB mapDb;

    private final String pre = "巴乐兔_";
    private final HTreeMap<String, Boolean> hash;

    @AndroidFindBy(xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[3]/android.widget.ImageView")
    private AndroidElement 我的;

    @AndroidFindBy(uiAutomator = """
                   new UiSelector().textMatches("签到")
                """)
    private AndroidElement 跳转到签到;

    /**
     * text: 签到抵房租
     */
    @AndroidFindBy(id = "com.haimai.baletu:id/tv_sign_in")
    private AndroidElement 签到按钮;

    @AndroidFindBy(id="com.haimai.baletu:id/iv_red_envelope")
    private AndroidElement 红包图片;

    public 巴乐兔AppiumTask(AndroidDriver driver) {
        this.driver = driver;
        this.kit = new AppiumUtils(driver);
        this.mapDb = SpringUtil.getBean(DB.class);
        this.hash = (HTreeMap<String, Boolean>) mapDb.hashMap("baletu").createOrOpen();

        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    @Override
    public String appName() {
        return "巴乐兔";
    }

    @Override
    public boolean isNeedRun() {
        String key = DateUtil.formatDate(new Date());
        log.info("{},key:{}", pre, key);
        if (Optional.ofNullable(hash.get(key)).orElse(false)) {
            log.info("今天已经签到过了");
            ThreadUtil.sleep(1,TimeUnit.SECONDS);
            return false;
        }
        return true;
    }

    @Override
    public void run(Date endTime) {
        this.关闭广告();
//        log.info("{}点击我的",pre);
//        baletu.get我的().click();
//        this.close弹层();

        log.info("{}跳转到签到",pre);
        跳转到签到.click();
        log.info("baletu.签到按钮.getText() = " + 签到按钮.getText());
        if (kit.isExist(签到按钮)) {
            String text = 签到按钮.getText();
            if ("签到抵房租".equals(text)) {
                签到按钮.click();
            }
        }

        ThreadUtil.sleep(1,TimeUnit.SECONDS);
        kit.click(红包图片,0,-150);
        log.info("拆红包");

        String key = DateUtil.formatDate(new Date());
        this.hash.put(key,true);
    }

//    public boolean close弹层(){
//        try {
//            ThreadUtil.sleep(1,TimeUnit.SECONDS);
//            AndroidElement 弹窗关闭按钮 = (MobileElement) driver.findElementById("com.haimai.baletu:id/baseui_dialog_iv_close");
//            log.info("!!!!关闭任务中没有每日签到弹窗");
//            弹窗关闭按钮.click();
//            return true;
//        } catch (Exception e) {
//            log.info("任务中没有每日签到弹窗");
//        }
//        return false;
//    }

    private boolean 关闭广告(){
        try {
            AndroidElement 弹窗关闭按钮 = (AndroidElement) driver.findElementByAccessibilityId("关闭广告");
            log.info("!!!!关闭弹窗");
            kit.click(弹窗关闭按钮,0,0);
//            弹窗关闭按钮.click();
            return true;
        } catch (Exception e) {
//            log.info("没有开心收下弹窗");
        }
        return false;
    }

    @Override
    public void openActivity() {
        driver.startActivity(mainActivity);
    }

    @Override
    public void checkAndInstallApk() {

    }
}
